﻿#pragma once

#include "BaseGlobal.h"
#include "RecordTable.h"
#include <QAbstractItemModel>
#include <QStyledItemDelegate>
#include <QComboBox>

//==================================================================================================
//自定义表模型

class BASE_EXPORT QRecordTableModel : public QAbstractItemModel
{
	Q_OBJECT

public:
	QRecordTableModel( QObject * parent = NULL );
	~QRecordTableModel( );

public:
	bool select(const QString & SQL);
	bool select(const QString & SQL, QString & Error);

	void addRow(const QList<QVariant> & value);
	void removeRow(int row);
	void addCol(const QString & name, COLUMN_TYPE type, COLUMN_FLAGS flags = CF_NONE, const QString & alias = "");
	void setCol( QString name, COLUMN_FLAGS flags, const QString & alias = "" );
	void setCol( int col, COLUMN_FLAGS flags, const QString & alias = "" );
	void setAt(int row, int col, QVariant value);
	
	void refresh( void );
	QRecordTable & records( )const;
	QModelIndex getModelByIndex( int rowIndex ) const;
	QModelIndex getFindIndex( int col, const QString & text );
	QVariant getItemData(int row,int col);

public:
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role ) const;
    virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex( ) ) const;
    virtual QVariant data( const QModelIndex &index, int role ) const;
	virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    virtual QModelIndex parent( const QModelIndex & child ) const;
    virtual int columnCount( const QModelIndex & parent = QModelIndex( ) ) const;
    virtual int rowCount( const QModelIndex & parent = QModelIndex( ) ) const;
	virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
	virtual QVariant display( int col , const QVariant & value ) const;

public:
	//修改数据
	virtual QModelIndex insertRow(QVariantList & value, bool bSeq = true);
	bool modifyRow(int row, int col, const QVariant & value);
	bool modifyRow(const QModelIndex & index, const QVariant & value);
	bool deleteRow(const QModelIndex & index);

	int saveData(QString & Error, bool Refresh = true);
	int cancelSaveData();
	int recordsChanged();	//数据源改变提醒。
	bool deleteRow(int index) const;

protected:
	mutable QRecordTable dsRecords;

};

class BASE_EXPORT QEditTableDelegate : public QStyledItemDelegate
{
	Q_OBJECT

public:

	QEditTableDelegate(QObject *parent = 0);
	~QEditTableDelegate();

	virtual void InitComboBox(const QModelIndex &index, QComboBox * editor) const;

	virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option,
		const QModelIndex & index) const;

	virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;

	virtual void setModelData(QWidget *editor, QAbstractItemModel *model,
		const QModelIndex &index) const;

	virtual void updateEditorGeometry(QWidget *editor,
		const QStyleOptionViewItem &option, const QModelIndex &index) const;

	virtual void paint(QPainter *painter,
		const QStyleOptionViewItem &option, const QModelIndex &index) const;

};
